import json
import time

import requests


def singer_detail(artist_id):
    # 获取用户详情信息
    author_url = 'https://v1.hitokoto.cn/nm/artist/' + str(artist_id)
    content = requests.get(author_url)
    rets = json.loads(content.text)
    detail = []
    name = rets['artist']['name']  # 歌手名
    pic = rets['artist']['picUrl']  # 歌手图片
    desc = rets['artist']['briefDesc']  # 歌手简介
    music_num = rets['artist']['musicSize']  # 单曲个数
    album_num = rets['artist']['albumSize']  # 专辑个数
    mv = rets['artist']['mvSize']  # MV个数
    # request.session['singer_img'] = pic
    # request.session['singer_name'] = name
    date = {
        'name': name,
        'pic': pic,
        'desc': desc,
        'music_num': music_num,
        'album_num': album_num,
        'mv': mv,
    }
    detail.append(date)
    b = 0
    i = 0
    lists = []
    try:
        while i <= 19:
            singer_name = rets['hotSongs'][1]['ar'][0]['name']  # 歌手姓名
            singer_id = rets['hotSongs'][1]['ar'][0]['id']  # 歌手id
            album_id = rets['hotSongs'][b]['al']['id']  # 专辑id
            album_name = rets['hotSongs'][b]['al']['name']  # 专辑名字
            song_name = rets['hotSongs'][b]['name']  # 单曲
            song_id = rets['hotSongs'][b]['id']  # 单曲id
            song_mv = rets['hotSongs'][b]['mv']  # 单曲mv

            i += 1
            b += 1
            dicts = {
                'singer_name': singer_name,
                'singer_id': singer_id,
                'album_id': album_id,
                'album_name': album_name,
                'song_name': song_name,
                'song_id': song_id,
                'song_mv': song_mv,
                'id': b
            }
            lists.append(dicts)
    except Exception as e:
        pass

    return detail, lists


def singer_album(artist_id):
    # 获取此歌手的专辑
    album_url = 'http://musicapi.leanapp.cn/artist/album?id=' + artist_id + '&limit=5'
    content = requests.get(album_url)
    ret = json.loads(content.text)
    album_list = []
    b = 0
    i = 0
    try:
        while i <= 10:
            album_pic = ret['hotAlbums'][i]['picUrl']
            album_name = ret['hotAlbums'][i]['name']
            album_id = ret['hotAlbums'][i]['id']
            times = ret['hotAlbums'][i]['publishTime']

            t = str(times)
            p = t[0:10]
            format = '%Y-%m-%d'
            value = time.localtime(int(p))
            album_times = time.strftime(format, value)
            i += 1
            b += 1
            dicts = {
                'album_name': album_name,
                'album_id': album_id,
                'album_pic': album_pic,
                'album_times': album_times,
            }
            album_list.append(dicts)
    except Exception as e:
        pass

    return album_list


def singer_mv(artist_id):
    # 歌手的mv
    mv_url = 'http://musicapi.leanapp.cn/artist/mv?id=' + artist_id + '&limit=5'
    content = requests.get(mv_url)
    ret = json.loads(content.text)
    mv_list = []
    b = 0
    i = 0
    try:
        while i <= 10:
            mv_pic = ret['mvs'][i]['imgurl']
            mv_name = ret['mvs'][i]['name']
            mv_id = ret['mvs'][i]['id']
            times = ret['mvs'][i]['publishTime']

            i += 1
            b += 1
            dicts = {
                'mv_name': mv_name,
                'mv_id': mv_id,
                'mv_pic': mv_pic,
                'mv_times': times,
            }
            mv_list.append(dicts)
    except Exception as e:
        pass

    return mv_list